package com.hz.cheetah.hub.zookeeper;

import com.hz.cheetah.common.net.NetUtil;
import com.hz.cheetah.hub.ServiceDiscovery;
import com.hz.cheetah.hub.protocol.CheetahURL;
import org.I0Itec.zkclient.IZkChildListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 基于 ZooKeeper 的服务发现接口实现
 *
 * @author yangchuan
 * @since 1.0.0
 */
public class OneWayServiceDiscovery implements ServiceDiscovery {


    private static final Logger logger = LoggerFactory.getLogger(OneWayServiceDiscovery.class);


    @Override
    public CheetahURL discover(String name) {
        String brokerId = OneWayContext.pull();
        String address = "cheetah://" + NetUtil.serverUID2StringIpPort(Long.valueOf(brokerId));
        CheetahURL cheetahURL = CheetahURL.pase2CURL(address);
        cheetahURL.init();
        return cheetahURL;
    }


    /**
     * 当第一次发现的服务器有问题后，重新发现发现一台服务器
     *
     * @param serviceName
     * @param old
     * @return
     */
    public CheetahURL reDiscover(String serviceName, CheetahURL old) {
        return discover(serviceName);
    }
}